<template>
    <div>
      <!--      表格位置-->
      <div class="button-table-box" v-loading="isLoading">
        <div class="info-in-box">
          <span class="title">姓名：</span>
          <el-input placeholder="输入师傅姓名" clearable @change="getDoctorLists" v-model="searchDoctor" style="width: 15%"></el-input>
          <span class="title" style="margin-left: 30px;">所在店铺：</span>
          <el-select v-model="selectMassageID" placeholder="请选择"  @change="getDoctorLists()" style="width: 15%;">
            <el-option
              v-for="item in selectMassageData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>

          <el-button type="primary" size="mini" style="margin-left: 30px;" @click="getDoctorLists()">查询</el-button>
        </div>
        <div class="button-box" style="margin-top: 20px;">
            <el-button type="primary"  size="small" icon="el-icon-circle-plus"
                       class="add-button" @click="addDoctor()">添加</el-button>
        </div>
        <table-list :tableAllData="tableAllData" @getTableData="getTableData" ref="tableList"></table-list>
      </div>
      <page-pagination :page-list="pageList" ref="pagePagination"></page-pagination>
      <!--    用作删除的弹框-->
      <delete-dialog :dialog="deleteDialog" ref="deleteDialog"></delete-dialog>
    </div>
</template>

<script>
  import {getMassageInfo} from "@/api/massage";
  import {getDoctorList, deleteDoctor, getDoctorById} from "@/api/doctor";
  import {getDepartmentList} from "@/api/department";
  import {getOutpatientListById} from "@/api/outpatient";
  import {tips} from "@/common/js/optionTips";
  import {createDoctorManager} from "@/api/doctor";
  import SHA256 from 'js-sha256';
  import { addOutCall} from "@/api/outCall";
  export default {
        name: "doctorDetail",
      data() {
          return {
            // 顶部筛选信息
            isLoading: false,
            searchDoctor: '',
            selectMassageData:[],
            selectMassageID: 0,
            selectDepartmentID: 10001,
            selectOutpatientID: 10001,
            // 表格数据
            tableAllData: {
              dataNull: false,
              tableTitle: [{
                prop: 'ID',
                label: '师傅ID',
                width: '100',
                option: ''
              }, {
                prop: 'name',
                label: '姓名',
                width: '130',
                option: ''
              }, {
                prop: 'gender',
                label: '性别',
                width: '100',
                option: ''
              },
                {
                prop: 'address',
                label: '所属店铺',
                width: '130',
                option: ''
              },
                {
                prop: 'specialty',
                label: '简介',
                width: '200',
                option: ''
              }],
              tableData: [],
              option: {
                width: '250',
                button: [{
                  size: 'mini',
                  type: 'primary',
                  name: '创建账号'
                }, {
                  size: 'mini',
                  type: 'warning',
                  name: '添加出诊'
                },{
                  size: 'mini',
                  type: 'success',
                  name: '编辑'
                }, {
                  size: 'mini',
                  type: 'danger',
                  name: '删除'
                }]
              }
            },
            // 底部分页的数据
            pageList: {
              pageNum: 1,
              pageSize: 5,
              total: 0
            },
            // 删除的模态框的数据
            deleteDialog: {
              title: '专科',
              dialogFormVisible: false,
              id: ''
            },
            deleteScope: {}, // 用作当从组件传过来确认删除时用的
          }
      },
      methods: {
          // 获取表格的点击按钮的信息反馈
        getTableData: function (option) {
          if (option.buttonName === '删除') {

            this.deleteScope = option; // 将删除的数据存起来
            this.deleteDialog.dialogFormVisible = true; // 打开删除弹窗
            this.deleteDialog.id = option.scopeRow.ID; // 发送删除id

          } else if (option.buttonName === '编辑') {
            console.log('option.scopeRow',option.scopeRow)
            let data = {
              ID:option.scopeRow.ID,
              address:option.scopeRow.address,
              gender:option.scopeRow.gender,
              name:option.scopeRow.name,
              picture:option.scopeRow.picture,
              specialty:option.scopeRow.specialty,
              status:option.scopeRow.status,
              massageId:this.selectMassageID
            }
            sessionStorage.setItem('doctorInfo', JSON.stringify(data));
            //console.log(option.scopeRow)
            this.$router.push({
              path: '/editDoctor'
            })
          } else if (option.buttonName === '创建账号') {
            this.isLoading = true;
            createDoctorManager({
              name: option.scopeRow.ID,
              //密码默认123456
              password: SHA256('123456')
            }).then(res => {
              if (res.code === 200) {
                this.isLoading = false;
                tips('success', '创建账号成功')
              }
            }).catch(() => {
              this.isLoading = false;
              tips('error', '账号已存在')
            })
          }else if(option.buttonName === '添加出诊'){
            let data = {
              clinicId:'10008',
              day:'1,2,3,4,5',
              doctorId:option.scopeRow.ID,
              duan:'08:40-09:20,09:20-10:00,10:00-10:40,10:40-11:20,11:20-12:00,14:00-14:40,14:40-15:20,15:20-16:00,16:00-16:40,16:40-17:20,17:20-18:00,18:00-18:40',
              massageId: this.selectMassageID,
              jia:'',
              outpatientId: '10001',
              specialId: '10001',
              time:1
            }
            addOutCall(data).then(res=>{
              if(res.code === 200){
                tips('success', '添加成功')
              }
            }).catch(()=>{
              tips('error','该师傅已添加出诊时间')
            })
          }
        },
        // 添加师傅信息
        addDoctor: function() {
          this.$router.push({
            path: '/addDoctor',
            query: {
              specialId: this.selectDepartmentID,
              outpatientId: this.selectOutpatientID
            }
          })
        },
        // 用户点击删除某条师傅信息
        deleteData: function () {
          deleteDoctor(this.deleteScope.scopeRow.ID).then(res => {
            //console.log('res',res)
            if (res.code === 200) {
              this.deleteDialog.dialogFormVisible = false;
              this.$refs.tableList.deleteData(this.deleteScope.scopeIndex)
            }
          }).catch((error) => {
            this.deleteDialog.dialogFormVisible = false;
            if(error === 'Error: Network Error'){
              tips('error', '删除失败,请检查网络')
            }else{
              tips('error', '该医生还存在出诊计划，请先删除')
            }
          });
        },
        // 判断性别
        getGender: function(id) {
          if (id === 1) {
            return '男'
          }
          return '女'
        },
        getDoctorLists: function (){
            this.tableAllData.tableData = []
            let that = this;
            getDoctorById(this.selectMassageID,this.searchDoctor,this.pageList.pageNum,this.pageList.pageSize).then(res => {
              if (res.code === 200){
                console.log('res',res)
                this.pageList = {
                  pageNum: res.data.pageNum,
                  pageSize: res.data.pageSize,
                  total: res.data.total
                };
                if (res.data.list.length === 0){
                  this.tableAllData.dataNull = true;
                  return;
                }
                res.data.list.forEach(function (item) {
                    that.tableAllData.tableData.push({
                    ID: item.id,
                    name: item.name,
                    gender: that.getGender(item.gender),
                    address: item.address,
                    specialty: item.specialty,
                    picture: item.img
                  })
                })
              } else {
                this.tableAllData.dataNull = true
              }
            })
        },

        // 获取所有店铺
        getAllMassage: function (){
          getMassageInfo(1,50).then(res=>{
            //console.log(res);
            //请求成功
            if (res.code === 200 && res.data.list.length > 0){
              this.selectMassageData = res.data.list;
              this.selectMassageID = this.selectMassageData[0].id;
              this.getDoctorLists();
            }
          }).catch(()=>{
            tips('error','获取店铺信息失败，请检查网络');
          })
        },
        // 子组件分页通过调用父组件的方法进行调用更新表格
        fatherMethod: function (pageNum, pageSize) {
          this.pageList.pageNum = pageNum;
          this.pageList.pageSize = pageSize;
          this.getDoctorList()
        }
      },
    created() {
      this.getAllMassage()
    },
  }
</script>

<style lang="scss">
  @import "../.././common/scss/common.scss";
  .button-table-box{
    @include width-margin(90%, 100%);
    margin-top: 15px;
  }
</style>
